package com.yuuzy.dao;

import com.alibaba.druid.util.StringUtils;
import com.yuuzy.model.BankCard;
import com.yuuzy.model.CreditCard;

import java.util.List;
@Deprecated
public class CreditCardDAO extends BankCardDAO<CreditCard> {

    /**
     * 查询信用卡信息
     *
     * @param creditCard 待查询的信用卡实体
     * @return 查询到的信用卡实体集合
     */
    @Override
    public List<CreditCard> queryBankCard(BankCard creditCard) {
        StringBuilder sb = new StringBuilder("select * from creditcard");
        if (!StringUtils.isEmpty(((CreditCard) creditCard).getCreditcard_id())) {
            //通过银行卡号来查询
            sb.append(" and creditcard_id like '%").append(((CreditCard) creditCard).getCreditcard_id()).append("%'");//支持模糊查询
        }
        if (creditCard.getClient_id() != null) {
            //通过客户id来查询
            sb.append(" and client_id like '%").append(creditCard.getClient_id()).append("%'");//支持模糊查询
        }
        String sql = sb.toString().replaceFirst("and", "where");
        return queryMulti(sql, CreditCard.class);
    }

    /**
     * 增加信用卡
     *
     * @param creditCard 增加的信用卡实体
     * @return 受影响的行数
     */
    @Override
    public int insertBankCard(BankCard creditCard) {
        String sql = "INSERT INTO finance.creditcard(creditcard_id,creditcard_password,creditcard_limit,creditcard_remain,client_id) VALUES (?,?,?,?,?)";//添加信用卡信息
        return update(sql, creditCard.getBankcard_id(), creditCard.getBankcard_password(), creditCard.getBankcard_limit(), creditCard.getBankcard_remain(), creditCard.getClient_id());
    }
    /**
     * 删除信用卡
     *
     * @param creditCard 待删除的信用卡实体
     * @return 受影响的行数
     */
    public int deleteBankCard(BankCard creditCard) {
        StringBuilder sb = new StringBuilder("DELETE FROM creditcard");//删除id相匹配的信用卡
        if (!StringUtils.isEmpty(((CreditCard) creditCard).getCreditcard_id())) {
            //如果id字段存在
            sb.append(" and creditcard_id = '").append(creditCard.getBankcard_id()).append("'");
        }
        String sql = sb.toString().replaceFirst("and", "where");
        return update(sql);
    }

    /**
     * 更改信用卡信息
     *
     * @param creditCard 待更改的信用卡实体
     * @return 受影响的行数
     */
    public int updateBankCard(BankCard creditCard) {
        //id不能更改
        StringBuilder sb = new StringBuilder("UPDATE creditcard SET");
        if (!StringUtils.isEmpty(((CreditCard) creditCard).getCreditcard_password())) {
            //如果密码字段存在
            sb.append(" ,creditcard_password = '").append(((CreditCard) creditCard).getCreditcard_password()).append("'");
        }
        if (((CreditCard) creditCard).getCreditcard_limit() != null) {
            //如果限额字段存在
            sb.append(" ,creditcard_limit = ").append(((CreditCard) creditCard).getCreditcard_limit());
        }
        if (((CreditCard) creditCard).getCreditcard_remain() != null) {
            //如果信用卡余额字段存在
            sb.append(" ,creditcard_remain = ").append(((CreditCard) creditCard).getCreditcard_remain());
        }
        String sql = sb.append(" WHERE creditcard_id = ? AND client_id = ?").toString().replaceFirst(",", "");
        return update(sql, creditCard.getBankcard_id(), creditCard.getClient_id());
    }
}
